package linter.linterjava.controller.article;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import linter.linterjava.POJO.DO.*;
import linter.linterjava.POJO.DTO.*;
import linter.linterjava.POJO.UtilDTO.PageInfoDTO;
import linter.linterjava.custom.ResultVO;
import linter.linterjava.mapper.CollectMapper;
import linter.linterjava.service.*;
import linter.linterjava.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static linter.linterjava.custom.CustomResultType.USER_NOT_EXIST;

/**
 * @author 北煜
 */
@Slf4j
@Api(tags = "文章相关操作模块")
@RestController
@RequestMapping("articleAbout")
public class ArticleAboutController {

    @Resource
    UserService userService;
    @Resource
    ArticleLikesService articleLikesService;
    @Resource
    CollectService collectService;
    @Resource
    ArticleCollectService articleCollectService;
    @Resource
    ArticleReviewService articleReviewService;
    @Resource
    ReviewLikesService reviewLikesService;
    @Resource
    ReviewDislikesService reviewDislikesService;
    @Resource
    ReviewReportService reviewReportService;
    private final UserQueryService userQueryService;
    @Autowired
    public ArticleAboutController(UserQueryService userQueryService) { this.userQueryService = userQueryService; }
    @GetMapping("/show/getUserByUsername/{username}")
    @ApiOperation("根据用户名查找用户")
    public ResultVO<UserDTO> getUserByUsername(@PathVariable String username) { return userQueryService.getUserByUsername(username); }


    // TODO: 这里还可以进行取消点赞，但是我没有写。
    @PostMapping("like")
    @ApiOperation("文章点赞")
    public ResultVO<String> like(@RequestParam Long articleId) {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(articleLikesService.like(uuid, articleId)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    /**
     * @param collectDTO 收藏类 - 收藏夹名称，收藏夹简介
     */
    @PostMapping("createCollect")
    @ApiOperation("创建收藏夹")
    public ResultVO<String> createCollect(@RequestBody CollectDTO collectDTO) {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(collectService.create(uuid, collectDTO)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("deleteCollect")
    @ApiOperation("删除收藏夹")
    public ResultVO<String> deleteCollect(@RequestParam Long collectId) {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(collectService.delete(uuid, collectId)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("collectArticle")
    @ApiOperation("收藏文章")
    public ResultVO<String> collectArticle(@RequestBody ArticleCollectDTO articleCollectDTO){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(articleCollectService.collect(uuid, articleCollectDTO)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("deleteCollectArticle")
    @ApiOperation("取消收藏")
    public ResultVO<String> deleteCollectArticle(@RequestBody ArticleCollectDTO articleCollectDTO) {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(articleCollectService.deleteCollect(uuid, articleCollectDTO)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("getCollect")
    @ApiOperation("获取收藏夹列表")
    public ResultVO<PageInfo<Collect>> getCollect(@RequestBody PageInfoDTO pageInfoDTO) {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(collectService.getCollect(uuid, pageInfoDTO)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("getCollectArticle")
    @ApiOperation("获取收藏夹文章列表")
    public ResultVO<PageInfo<Article>> getCollectArticle(@RequestBody ArticleCollectDTO articleCollectDTO){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(articleCollectService.getCollectInArticle(uuid, articleCollectDTO)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    /**
     * @param articleReviewDTO 评论类 -- 父节点评论可以为null；
     */
    @PostMapping("addReview")
    @ApiOperation("文章评论")
    public ResultVO<String> addComment(@RequestBody ArticleReviewDTO articleReviewDTO){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(articleReviewService.addComment(uuid, articleReviewDTO)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    /**
     * @param articleReviewDTO 评论类 -- 被回复的评论id，评论内容，所属文章id
     */
    @PostMapping("reviewComment")
    @ApiOperation("回复评论")
    public ResultVO<String> reviewComment(@RequestBody ArticleReviewDTO articleReviewDTO) {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null) {
            return ResultVO.success(articleReviewService.reviewComment(uuid, articleReviewDTO));
        }
        return ResultVO.error(USER_NOT_EXIST);
    }


    @PostMapping("getReview")
    @ApiOperation("获取文章评论")
    public ResultVO<PageInfo<ArticleReview>> getReview(@RequestBody ArticleReviewDTO articleReviewDTO){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(articleReviewService.getReview(uuid, articleReviewDTO)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    /**
     * TODO: 点赞和踩都没有写取消
     *
     * @param reviewLikesDTO 评论点赞类 -- 被点赞的评论id，点赞用户id
     */
    @PostMapping("likeReview")
    @ApiOperation("评论点赞")
    public ResultVO<String> likeReview(@RequestBody ReviewLikesDTO reviewLikesDTO){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(reviewLikesService.likeReview(uuid, reviewLikesDTO)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("dislikeReview")
    @ApiOperation("踩评论")
    public ResultVO<String> dislikeReview(@RequestBody ReviewDislikesDTO reviewDislikesDTO){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(reviewDislikesService.dislikeReview(uuid, reviewDislikesDTO)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("reportReview")
    @ApiOperation("举报评论")
    public ResultVO<String> reportReview(@RequestBody ReviewReportDTO reviewReportDTO){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(reviewReportService.reportReview(uuid, reviewReportDTO)); }
        return ResultVO.error(USER_NOT_EXIST);
    }





}
